﻿using System;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Collections.Generic;
using System.Collections;
using SacNetUG.Helpers;
using SacNetUG.Models;

namespace SacNetUG.Controllers
{
    public delegate ActionResult ReturnDelegate(string viewName);

    public interface IWfStep
    {
        string ActionNext(int? id, FormCollection collection, ViewDataDictionary viewData, TempDataDictionary tempData, HttpRequestBase request);
        string ActionBack(int? id, FormCollection collection, ViewDataDictionary viewData, TempDataDictionary tempData, HttpRequestBase request);
        string WfStepName { get; }
    }

    public class WfManagerBase
    {

        private FormCollection collection;
        private int? id;
        private ViewDataDictionary viewData;
        private TempDataDictionary tempData;
        private HttpRequestBase request;

        private IWfStep currentStep;
        private LinkedList<IWfStep> wfStateList = new LinkedList<IWfStep>();

        private Hashtable persistedValues = new Hashtable();
        
        public enum UserAction
        {
            Next = 1,
            Back = 2,
            Cancel = 3,
            Undefined = 4
        }

        //Probably don't need int? id but in the interest of flexibility...
        public WfManagerBase(int? id, FormCollection collection, ViewDataDictionary viewData, TempDataDictionary tempData, HttpRequestBase request)
        {
            this.id = id;
            this.collection = collection;
            this.viewData = viewData;
            this.tempData = tempData;
            this.request = request;
        }

        public void AddWfStep(IWfStep step)
        {
            if (wfStateList.Count == 0)
            {
                wfStateList.AddFirst(step);
            }
            else
            {
                wfStateList.AddAfter(wfStateList.Last, step);
            }

        }

        public object GetPersistedData(string dictionaryName, string key)
        {
            Hashtable ht = (Hashtable)persistedValues[dictionaryName];
            return ht[key];
        }

        public object GetPersistedData(string key)
        {
            return persistedValues[key];
        }

        private IWfStep GetState(string wfStepName)
        {
            foreach (IWfStep state in wfStateList)
            {
                if (state.WfStepName == wfStepName)
                {
                    return state;
                }
            }

            return wfStateList.First.Value;
        }

        //TODO: find a good way to allow controller to return a view or redirecttoaction. 
        //TODO: rename this as it does not imply the work being done
        //TODO: Consolidate into one delegate and pass viewname and action to take
        public ActionResult GetViewName(ReturnDelegate returnView, ReturnDelegate returnRedirect)
        {
            if (tempData.ContainsKey("WfPersistedData"))
            {
                persistedValues = (Hashtable) tempData["WfPersistedData"];
            }

            //Find previous step
            if (tempData.ContainsKey("WfStepName"))
            {
                currentStep = GetState(tempData["WfStepName"].ToString());
            }
            else
            {
                //load the first step
                currentStep = wfStateList.First.Value;
            }

            UserAction userAction = GetUserAction(collection);

            string viewName = "";

            switch (userAction)
            {
                case UserAction.Next:
                    currentStep = wfStateList.Find(currentStep).Next.Value;
                    viewName = currentStep.ActionNext(id, collection, viewData, tempData, request);
                    break;

                case UserAction.Back:
                    currentStep = wfStateList.Find(currentStep).Previous.Value;
                    viewName = currentStep.ActionBack(id, collection, viewData, tempData, request);
                    break;

                case UserAction.Cancel:
                    //Define cancel at a WF level?
                    break;

                case UserAction.Undefined:
                    //first step first action
                    viewName = currentStep.ActionNext(id, collection, viewData, tempData, request);
                    break;
                    
            }


            tempData.Clear();
            persistedValues.AddOrReplace(string.Format("{0}.FormValues", currentStep.WfStepName), collection);
            tempData.AddOrReplace("WfPersistedData", persistedValues);
            tempData.AddOrReplace("WfStepName", currentStep.WfStepName);
            if (viewName == "Index")
            {
                return returnRedirect(viewName);
            }
            else
            {
                return returnView(viewName);
            }
            
        }

        public void PersistData(string dictionaryName, string key, object value)
        {
            if (persistedValues.ContainsKey(dictionaryName))
            {
                Hashtable ht = (Hashtable) persistedValues[dictionaryName];
                if (ht.ContainsKey(key))
                {
                    ht[key] = value;
                }
                else
                {
                    ht.Add(key, value);
                }
            }
            else
            {
                Hashtable ht = new Hashtable();
                ht.Add(key, value);
                persistedValues.Add(dictionaryName, ht);
            }
        }

        public void PersistData(string key, object value)
        {
            persistedValues.AddOrReplace(key, value);
        }

        private UserAction GetUserAction(FormCollection collection)
        {
            if (!string.IsNullOrEmpty(collection["Next"]))
            {
                return UserAction.Next;
            }
            if (!string.IsNullOrEmpty(collection["Back"]))
            {
                return UserAction.Back;
            }
            if (!string.IsNullOrEmpty(collection["Cancel"]))
            {
                return UserAction.Cancel;
            }
            return UserAction.Undefined;
        }
    }

    public class WfStepBase : IWfStep
    {
        protected WfManagerBase wf;
        private string wfStepName = "";

        public WfStepBase(WfManagerBase wf, string wfStepName)
        {
            this.wfStepName = wfStepName;
            this.wf = wf;
        }
        public WfStepBase(WfManagerBase wf)
        {
            Type thisType = this.GetType();
            this.wf = wf;
            this.wfStepName = thisType.Name;
        }

        public virtual string WfStepName
        {
            get { return wfStepName; }
        }

        public virtual string ActionNext(int? id, FormCollection collection, ViewDataDictionary viewData, TempDataDictionary tempData, HttpRequestBase request)
        {
            return "";
        }

        public virtual string ActionBack(int? id, FormCollection collection, ViewDataDictionary viewData, TempDataDictionary tempData, HttpRequestBase request)
        {
            return "";
        }

    }




}








